package com.my.dao.impl;

import java.math.BigInteger;
import java.util.List;

import com.my.bean.OrderBack;
import com.my.dao.IBaseDAO;
import com.my.dao.OrderBackDAO;

public class OrderBackDAOImpl implements OrderBackDAO {
	private IBaseDAO<OrderBack> basedao;

	public void setBasedao(IBaseDAO<OrderBack> basedao) {
		this.basedao = basedao;
	}

	boolean checkOrderBack(OrderBack orderback) {
		if (orderback == null || orderback.getOrderaccepted() == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public boolean addOrderBack(OrderBack orderback) throws Exception {
		if (this.checkOrderBack(orderback)) {
			return basedao.add(orderback);
		}
		return false;
	}

	@Override
	public OrderBack getOrderBackById(Class<OrderBack> c, Integer id)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BigInteger getTotalOrderBackRecords() throws Exception {
		String sql = "select count(order_back_id) from info_order_back";
		return basedao.getTotalRecords(sql);
	}

	@Override
	public List<OrderBack> getAllOrderBackByPage(int pageindex, int pagesize)
			throws Exception {
		String hql = "from OrderBack";
		return basedao.getAllEntityByPage(hql, pageindex, pagesize);
	}

	@Override
	public BigInteger searchTotalOrderBackRecords(OrderBack orderback)
			throws Exception {
		String sql = "select count(a.order_back_id) from info_order_back a inner join info_order_accepted d on d.order_accepted_id=a.order_accepted_id inner join info_order b on b.order_id=d.order_id inner join info_user c on c.user_id=b.user_id  where c.province like ? and c.city like ? and b.order_no like ? ";
		String item1 = orderback.getOrderaccepted().getOrder().getUser()
				.getProvince()
				+ "%";
		String item2 = orderback.getOrderaccepted().getOrder().getUser()
				.getCity()
				+ "%";
		String item3 = orderback.getOrderaccepted().getOrder().getNo() + "%";
		return basedao.getTotalRecords(sql, item1, item2, item3);
	}

	@Override
	public List<OrderBack> searchOrderBackByPage(int pageindex, int pagesize,
			OrderBack orderback) throws Exception {
		String hql = "from OrderBack as o where o.orderaccepted.order.user.province like ? and o.orderaccepted.order.user.city like ? and o.orderaccepted.order.no like ?";
		String item1 = orderback.getOrderaccepted().getOrder().getUser()
				.getProvince()
				+ "%";
		String item2 = orderback.getOrderaccepted().getOrder().getUser()
				.getCity()
				+ "%";
		String item3 = orderback.getOrderaccepted().getOrder().getNo() + "%";
		return basedao.getAllEntityByPage(hql, pageindex, pagesize, item1,
				item2, item3);
	}

}
